package yz.cp.back.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yz.cp.back.common.base.BaseController;
import yz.cp.back.common.enums.RoleType;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.common.utils.PageKit;
import yz.cp.back.u.entity.UserLogin;
import yz.cp.back.u.entity.UserRole;
import yz.cp.back.u.service.IUserLoginService;
import yz.cp.back.u.service.IUserRoleService;
import yz.cp.back.user.service.UserRoleMange;
import yz.cp.back.user.vo.UserLoginQueryVo;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web/userLogin")
public class UserLoginController extends BaseController {

    public UserRole userRoleConstant = new UserRole();
    @Autowired
    private IUserLoginService userLoginService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private UserRoleMange userRoleMange;

    @PostMapping("getByCondition")
    public ApiResult list(UserLoginQueryVo vo) {
        Map map = beanMapper.map(vo, Map.class);
        IPage<UserLogin> page = userLoginService.findByCondition(PageKit.buildPage(vo), map);

        if (page.getRecords() != null && page.getRecords().size() > 0) {
            List<String> roleList = Lists.newArrayList();
            page.getRecords().forEach(v ->
                    roleList.add(v.getId())
            );
            List<UserRole> userRoleList = userRoleMange.getAccessUserRoleByUserId(roleList);
            page.convert(v -> {
                Map m = beanMapper.map(v, Map.class);
                List<Map> l = Lists.newArrayList();
                String roleType = "";
                for (UserRole userRole : userRoleList) {
                    if (StringUtils.equalsIgnoreCase(userRole.getUserId(), v.getId())) {
                        Map map2 = Maps.newHashMap();
                        map2.put("accessName", userRole.getAccessName());
                        map2.put("merchantName", userRole.getMerchantName());
                        map2.put("merchantAbbrName", userRole.getMerchantAbbrName());
                        map2.put("id", userRole.getId());
                        map2.put("accessId", userRole.getAccessId());
                        roleType = RoleType.getByCode(userRole.getRoleType()).getName();
                        l.add(map2);
                    }
                }
                m.put("roleTypeName", roleType);
                m.put("roles", l);
                return m;
            });
        }
        return renderOk().add(page);
    }

    @RequestMapping(value = "/disable/{id}", method = RequestMethod.POST)
    public ApiResult disable(@PathVariable String id) {
        UserLogin userLogin = new UserLogin();
        userLogin.setId(id);
        userLogin.setStatus(1);
        userLoginService.updateById(userLogin);
        return renderOk("操作成功");
    }

    @RequestMapping(value = "/enable/{id}", method = RequestMethod.POST)
    public ApiResult enable(@PathVariable String id) {
        UserLogin userLogin = new UserLogin();
        userLogin.setId(id);
        userLogin.setStatus(0);
        userLoginService.updateById(userLogin);
        return renderOk("操作成功");
    }

}
